package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Orders;
import com.itheima.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * Rest模式
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询
     * @param page 当前页码
     * @param pageSize 每页显示的条数
     * @param number 订单号
     * @param beginTime 订单开始时间
     * @param endTime 订单结束时间
     * @return 分页数据
     */
    @GetMapping("page")
    public R<Page> page(Integer page, Integer pageSize, String number, String beginTime,String endTime){
        //分页构造器
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> ordersQueryWrapper = new LambdaQueryWrapper<>();
        //模糊查询条件
        ordersQueryWrapper.like(StringUtils.isNotBlank(number),Orders::getNumber,number);
        //范围查询
        ordersQueryWrapper.ge(StringUtils.isNotBlank(beginTime),Orders::getOrderTime,beginTime).le(StringUtils.isNotBlank(endTime),Orders::getCheckoutTime,endTime);
        //分页查询
        ordersService.page(ordersPage,ordersQueryWrapper);

        return R.success(ordersPage);
    }
}
